package com.rzico.sbl.ui.statistic

import androidx.core.view.isVisible
import com.rzico.sbl.R
import com.rzico.sbl.databinding.ActivityTicketGiveBinding
import com.rzico.sbl.model.TicketChangeData
import com.rzico.sbl.other.*
import com.rzico.sbl.ui.member.MemberDetailActivity
import com.rzico.sbl.viewmodel.StatisticCommonViewModel
import com.xinnuo.common.extend.*
import com.xinnuo.common.helper.getTimeString
import com.xinnuo.common.decoration.SpaceTBDecoration
import com.xinnuo.common_ui.base.BaseActivity
import com.xinnuo.common_ui.base.addItems
import com.xinnuo.common_ui.base.loadLinear
import com.xinnuo.common_ui.utils.showTimeRangeDialog
import com.xinnuo.slimadapter.SlimAdapter

class TicketGiveActivity : BaseActivity(R.layout.activity_ticket_give) {

    private val mBinding by lazy { ActivityTicketGiveBinding.bind(getRootView()) }

    override fun getViewModel() = get<StatisticCommonViewModel>()

    private lateinit var mDateMode: String
    private lateinit var mBeginDate: String
    private lateinit var mEndDate: String

    override fun initData() {
        initTitle(title = "赠送记录")

        initLayout()

        mBinding
            .listLayout
            .swipeRefresh
            .isRefreshing = true

        getData(pageNum)
    }

    private fun initLayout() {
        mBinding
            .listLayout
            .apply {
                emptyLayout.also {
                    it.emptyHint.text = "暂无相关水票记录！"
                }

                recycleList.also { view ->
                    view.loadLinear(
                        refresh = swipeRefresh,
                        onRefresh = { getData(0) },
                        onLoad = {
                            isLoadingMore
                                .conditionFalse {
                                    isLoadingMore = true
                                    getData(pageNum)
                                }
                        }
                    )

                    view.addItemDecoration(SpaceTBDecoration(hasHeader = false))

                    mAdapter = SlimAdapter.creator()
                        .register<TicketChangeData>(R.layout.item_statistic_ticket_1) { jector, bean, _ ->
                            jector.apply {
                                text(
                                    R.id.item_ticket_title,
                                    "${bean.memo}${bean.name.ifNotEmpty { "($it)" }}"
                                )
                                text(R.id.item_ticket_time, bean.createDate.getTimeString())
                                text(R.id.item_ticket_hint, "操作人：${bean.operator.orEmpty()}")
                                text(R.id.item_ticket_num, "+${bean.amount}")

                                clicked {
                                    startActivityEx<MemberDetailActivity>(
                                        "cardId" to bean.cardId,
                                        "memberId" to bean.memberId
                                    )
                                }
                            }
                        }
                        .attachTo(view)
                }
            }
    }

    override fun initListener() {
        mDateMode = intent.getExtra("dateMode")
        mBeginDate = intent.getExtra("beginDate")
        mEndDate = intent.getExtra("endDate")

        // 今日、昨日、本周、本月、上月、本年、自定义
        mBinding.apply {
            when (mDateMode) {
                "2" -> {
                    ticketTime.timeHint.text = "昨日"
                    getViewModel().mUncheckTimeList[1].isChecked = true
                }
                "3" -> {
                    ticketTime.timeHint.text = "本周"
                    getViewModel().mUncheckTimeList[2].isChecked = true
                }
                "4" -> {
                    ticketTime.timeHint.text = "本月"
                    getViewModel().mUncheckTimeList[3].isChecked = true
                }
                "5" -> {
                    ticketTime.timeHint.text = "上月"
                    getViewModel().mUncheckTimeList[4].isChecked = true
                }
                "6" -> {
                    ticketTime.timeHint.text = "本年"
                    getViewModel().mUncheckTimeList[5].isChecked = true
                }
                "7" -> {
                    ticketTime.timeHint.text = "$mBeginDate ~ $mEndDate"
                    getViewModel().mUncheckTimeList[6].isChecked = true
                }
                else -> {
                    mBeginDate = getTodayDate()
                    mEndDate = getTodayDate()

                    ticketTime.timeHint.text = "今日"
                    getViewModel().mUncheckTimeList[0].isChecked = true
                }
            }
        }

        mBinding.apply {
            ticketTime.apply {
                timeLayout.oneClick {
                    getViewModel()
                        .showDropTimeFilter(
                            getIContext(),
                            timeHint,
                            timeHintArrow,
                            timeLayout,
                            getViewModel().mUncheckTimeList
                        ) { bean ->

                            (bean.id != "7")
                                .condition {
                                    getViewModel()
                                        .mUncheckTimeList
                                        .last()
                                        .name = "自定义时间"
                                }

                            when (bean.id) {
                                "1" -> {
                                    mBeginDate = getTodayDate()
                                    mEndDate = getTodayDate()

                                    updateList()
                                }
                                "2" -> {
                                    mBeginDate = getBeforeDate()
                                    mEndDate = getBeforeDate()

                                    updateList()
                                }
                                "3" -> {
                                    mBeginDate = getMondayDate()
                                    mEndDate = getSundayDate()

                                    updateList()
                                }
                                "4" -> {
                                    mBeginDate = getFirstDate()
                                    mEndDate = getEndDate()

                                    updateList()
                                }
                                "5" -> {
                                    mBeginDate = getBeforeFirstDate()
                                    mEndDate = getBeforeEndDate()

                                    updateList()
                                }
                                "6" -> {
                                    mBeginDate = getYearFirstDate()
                                    mEndDate = getYearEndDate()

                                    updateList()
                                }
                                "7" -> {
                                    showTimeRangeDialog(
                                        isCurrentDate = true,
                                        isAfterLimited = true,
                                        onResult = { start, end ->
                                            getViewModel()
                                                .mUncheckTimeList
                                                .forEach { it.isChecked = false }

                                            getViewModel()
                                                .mUncheckTimeList
                                                .last()
                                                .isChecked = true

                                            mBeginDate = start
                                            mEndDate = end
                                            bean.name = "$mBeginDate ~ $mEndDate"
                                            timeHint.text = bean.name

                                            updateList()
                                        }
                                    )
                                }
                            }
                        }
                }
            }
        }
    }

    /**
     * 水票赠送记录列表
     */
    override fun getData(index: Int, isLoading: Boolean) {
        getViewModel()
            .ticketChange(
                page = index,
                type = "gift",
                goodsId = intent.getExtra("goodsId"),
                method = "",
                beginDate = mBeginDate,
                endDate = mEndDate,
                onSuccess = {
                    mList.apply {
                        (index == 0)
                            .condition {
                                clear()
                                pageNum = 0
                            }

                        addItems(it.data)
                        it.data.notEmpty { pageNum++ }
                    }

                    mBinding.apply {
                        ticketHint.text = "赠送数："
                        ticketCount.text = it.summary?.subQuantity.orEmpty("0")
                    }

                    mAdapter.setDataList(mList)
                },
                onFinally = {
                    isLoadingMore = false

                    mBinding
                        .listLayout
                        .apply {
                            swipeRefresh.isRefreshing = false

                            emptyLayout
                                .emptyView
                                .isVisible = mList.isEmpty()
                        }
                }
            )
    }

    /**
     * 刷新列表数据
     */
    private fun updateList() {
        mBinding
            .listLayout
            .apply {
                swipeRefresh.isRefreshing = true

                emptyLayout
                    .emptyView
                    .gone()
            }

        mList.notEmpty {
            mList.clear()
            mAdapter.notifyDataSetChanged()
        }

        getData(0)
    }

}